package graph

func findJudge(n int, trust [][]int) int {
	guyBeTrusted := map[int]int{} // 相信key的人数
	guyTrusted := map[int]int{}   // key相信的人数

	for _, v := range trust {
		if _, ok := guyBeTrusted[v[0]]; !ok {
			guyBeTrusted[v[0]] = 0
		}
		if _, ok := guyBeTrusted[v[1]]; !ok {
			guyBeTrusted[v[1]] = 0
		}
		if _, ok := guyTrusted[v[0]]; !ok {
			guyTrusted[v[0]] = 0
		}
		if _, ok := guyTrusted[v[1]]; !ok {
			guyTrusted[v[1]] = 0
		}

		guyTrusted[v[0]]++
		guyBeTrusted[v[1]]++
	}

	for i := 1; i <= n; i++ {
		if guyBeTrusted[i] == n-1 && guyTrusted[i] == 0 {
			return i
		}
	}
	return -1
}
